package com.electromagnetic.industry.software.manage.controller;

import com.electromagnetic.industry.software.common.annotations.UserOperation;
import com.electromagnetic.industry.software.common.enums.UserOperationModuleEnum;
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
import com.electromagnetic.industry.software.manage.pojo.req.AddImportTableDataDTO;
import com.electromagnetic.industry.software.manage.pojo.req.DownloadImportTableDataDTO;
import com.electromagnetic.industry.software.manage.pojo.req.UpdateImportTableDataDTO;
import com.electromagnetic.industry.software.manage.service.ImportTableService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/data/ed/struct/table")
public class ImportTableController {

    @Resource
    private ImportTableService importTableService;

    @GetMapping("/query")
    @UserOperation(value = "查询结构化数据", modelName = UserOperationModuleEnum.STRUCT_DATA)
    public ElectromagneticResult<?> query(@RequestParam String tableInfoId, @RequestParam Integer pageSize, @RequestParam Integer pageNum) {
        pageNum = Optional.ofNullable(pageNum).orElse(1);
        pageSize = Optional.ofNullable(pageSize).orElse(10);
        return ElectromagneticResultUtil.success(importTableService.selectTableDataByTableInfoId(pageNum, pageSize, tableInfoId));
    }

    @PostMapping("/add")
    @UserOperation(value = "新增结构化数据", modelName = UserOperationModuleEnum.STRUCT_DATA)
    public ElectromagneticResult<?> add(@RequestBody AddImportTableDataDTO addImportTableDataDTO) {
        return ElectromagneticResultUtil.success(importTableService.addTableData(addImportTableDataDTO.getTableInfoId(), addImportTableDataDTO.getTableData()));

    }

    @GetMapping("/remove")
    @UserOperation(value = "删除结构化数据", modelName = UserOperationModuleEnum.STRUCT_DATA)
    public ElectromagneticResult<?> remove(@RequestParam String id) {
        return importTableService.removeTableDataById(id);
    }

    @PostMapping("/update")
    @UserOperation(value = "更新结构化数据", modelName = UserOperationModuleEnum.STRUCT_DATA)
    public ElectromagneticResult<?> update(@RequestBody UpdateImportTableDataDTO updateImportTableDataDTO) {
        return importTableService.updateTableData(updateImportTableDataDTO.getId(), updateImportTableDataDTO.getTableData());
    }

    @PostMapping("/download")
    @UserOperation(value = "下载结构化数据", modelName = UserOperationModuleEnum.STRUCT_DATA)
    public ResponseEntity<byte[]> download(@RequestBody DownloadImportTableDataDTO downloadImportTableDataDTO, HttpServletResponse response) {
        return importTableService.downloadTableData(downloadImportTableDataDTO, response);
    }
}
